package com.kasax.skin.support;

import android.app.Activity;
import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.kasax.skin.support.utils.LogUtils;

import java.lang.reflect.Constructor;
import java.util.HashMap;
import java.util.Observable;
import java.util.Observer;

public class SkinLayoutFactory implements LayoutInflater.Factory2, Observer {
    private static final String[] mClassPrefixlist = {
            "android.widget.",
            "android.view.",
            "android.webkit."
    };
    private static final Class[] mConstructorSignature =
            new Class[]{Context.class, AttributeSet.class};

    private static final HashMap<String, Constructor<? extends View>> mConstructor =
            new HashMap<String, Constructor<? extends View>>();

    private final SkinAttribute skinAttribute;
    private final Activity activity;

    public SkinLayoutFactory(Activity activity, Typeface skinTypeFace) {
        this.activity = activity;
        skinAttribute = new SkinAttribute(skinTypeFace);
    }

    @Nullable
    @Override
    public View onCreateView(@Nullable View parent, @NonNull String name, @NonNull Context context, @NonNull AttributeSet attrs) {
        LogUtils.i(getClass(), "onCreateView activity-name: " + activity);
        // 反射 classLoader
        View view = createViewFromTag(name, context, attrs);
        // 自定义View
        if (null == view) {
            view = createView(name, context, attrs);
        }
        //    //筛选符合属性View
        skinAttribute.load(view, attrs);
        return view;
    }

    private View createViewFromTag(String name, Context context, AttributeSet attrs) {
        // 包含自定义控件
        if (-1 != name.indexOf(".")) {
            return null;
        }
        View view = null;
        for (int i = 0; i < mClassPrefixlist.length; i++) {
            view = createView(mClassPrefixlist[i] + name, context, attrs);
            if (null != view) {
                break;
            }
        }
        return view;
    }

    private View createView(String name, Context context, AttributeSet attrs) {
        Constructor<? extends View> constructor = mConstructor.get(name);
        if (constructor == null) {
            try {
                //通过全类名获取class
                Class<? extends View> aClass = context.getClassLoader().loadClass(name).asSubclass(View.class);
                // 获取构造方法
                constructor = aClass.getConstructor(mConstructorSignature);
            } catch (Exception e) {
                LogUtils.e(getClass(), e);
            }
        }
        if (null != constructor) {
            // 要两个参数的构建方法
            try {
                return constructor.newInstance(constructor, attrs);
            } catch (Exception e) {
                LogUtils.e(getClass(), e);
            }
        }
        return null;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull String name, @NonNull Context context, @NonNull AttributeSet attrs) {
        LogUtils.i(getClass(), "onCreateView-2 activity-name: " + activity);
        return null;
    }

    @Override
    public void update(Observable o, Object arg) {
        LogUtils.i(getClass(), "update activity-name: " + activity);
    }
}
